package cn.pli.bike.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.provider.Settings;

/**手机定位类相关服务
 * Created by Edward on 2017/11/8.
 */

public class LocationSettingUtil {

    private LocationSettingUtil(){}

    /**
     * 判断是否启动定位服务
     * @return
     */
    public static boolean isLocServiceOpen(Context context){
        boolean isGPS = false;
        boolean isNET = false;
        if(context != null){
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            if(lm != null){
                //通过GPS卫星定位是否开启
                isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
                //通过WLAN或移动网络(3G/2G)是否开启
                isNET = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            }
            if(isGPS || isNET){
                return true;
            }
        }
        return false;
    }

    /**
     * 跳转到开启定位服务界面
     */
    public static void goLocServiceSetting(Context context){
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//单独开启一个线程
        context.startActivity(intent);
    }

    /**
     * 跳转到开启定位服务界面
     */
    public static void goLocServiceSetting(Activity activity, int reqCode){
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activity.startActivityForResult(intent, reqCode);
    }

}
